package com.abbas.abchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.dao.impl.AccountDaoImpl;
import com.abbas.abchat.db.Sqlite;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.utils.SpUtils;
import com.abbas.abchat.utils.TextLengthUtils;

public class LoginActivity extends BaseActivity {
    private ImageButton ibBack;
    private EditText etAccount;
    private EditText etPassword;
    private Button btLogin;

    private AccountDao accountDao;

    @Override
    protected void init() {
        ibBack = (ImageButton) findViewById(R.id.ib_back);
        etAccount = (EditText) findViewById(R.id.et_account);
        etPassword = (EditText) findViewById(R.id.et_password);
        btLogin = (Button) findViewById(R.id.bt_login);

        accountDao = new AccountDaoImpl(this);
    }

    @Override
    protected void initEvent() {
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishThis();
            }
        });

        /**
         * in database find account and match the account password
         */
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password) &&
                        TextLengthUtils.isOverTo(account,TextLengthUtils.APMinLength) &&
                        TextLengthUtils.isOverTo(password,TextLengthUtils.APMinLength) &&
                        !TextLengthUtils.isLessOf(account,TextLengthUtils.APMaxLength) &&
                        !TextLengthUtils.isLessOf(password,TextLengthUtils.APMaxLength) ){
                    try {
                        final Account byAccount = accountDao.findByAccount(account);
                        if (byAccount != null){
                            if (password.equals(byAccount.getPassword())){
                                //TODO save the account in app running process of application
                                AbChatApplication.setAccount(byAccount);
                                //TODO save the account to SP
                                SpUtils.config(LoginActivity.this,SpUtils.LOGIN_ACCOUNT).putAccount(byAccount);
                                //TODO intent to home
                                migrateTo(HomeActivity.class);
                                //TODO need finish splashActivity
                                AbChatApplication.finishActivity(SplashActivity.class.getSimpleName());
                                //TODO finish this activity
                                finishThis();
                            }else {
                                showMessage(getResources().getString(R.string.loginError));
                            }
                        }else {
                            showMessage(getResources().getString(R.string.loginError));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    showMessage(getResources().getString(R.string.apFormatError));
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}